package com.pug.service.order;

import com.pug.mapper.message.UserMqRepository;
import com.pug.pojo.UserOrder;
import com.pug.vo.OrderPayVo;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * Author: lqm
 * create: 2022/5/11 23:03
 * Lover: lqx
 *
 * @see
 */
@Slf4j
@Component
public class GiveUpOrder {
    @Resource
    UserOrderServiceImpl userOrderService;
    public static final String LIU_ORDER_USER_QUEUE="liu.order.user.queue_a";
    /**
     *用户下订单30分钟之后，在这查询订单信息，如果用户的订单还没支付的话，就取消订单啦
     */
    @Qualifier("redisTemplate")
    @Autowired
    RedisTemplate<String,Object> redisTemplate;
    @Resource
    UserMqRepository userMqRepository;
    //处理来自用户模块用户信息登录信息的保存
    @RabbitListener(queues = {"liu.order.user.queue_a"},
            containerFactory = "simpleTopicOrderDeadContainerAuto")
    public void consumerOrder(@Payload OrderPayVo orderPayVo, Message message, Channel channel) {
        try {
            //这个地方得加一把锁
            String hotelId = orderPayVo.getHotelId();
            String hotelDetailId = orderPayVo.getHotelDetailId();
            UserOrder userOrder = userOrderService.queryNoPay(new Long(hotelId),new Long(hotelDetailId));
            if(userOrder.getStatus()==0){
                userOrder.setStatus(-1);
            }
        } catch (Exception ex) {
            log.error("下单出错保存出错：{}", orderPayVo);
        }
    }
}
